﻿
var errorText = "<div style='padding-top:75px' id='{divID}_{hrefID}'><div><span style='color:red'>图片加载失败</span><a href='#' onclick='ReloadImage(\"{divID}\",\"{hrefID}\",\"{imgID}\")'>重新加载</a></div>"
                + "<div>52icon.net</div></div>";
//重新加载错误图片
function ReloadImage(divID, hrefID, imgID) {
    var errorTextID = divID + "_" + hrefID;
    var errorTextDiv = document.getElementById(errorTextID);
    if (errorTextDiv != null) errorTextDiv.style.display = "none";
    //图片超链接DIV
    var hrefObj = document.getElementById(hrefID);
    if (hrefObj != null) hrefObj.style.display = "";

    //图片对象，刷新重新加载图片
    var imgObj = document.getElementById(imgID);
    if (imgObj != null) {
        imgObj.onerror = function () { ImageError(this); };
        var loadingImage = imgObj.getAttribute("loadingImage");
        var targetSrc = imgObj.getAttribute("targetSrc");
        imgObj.src = loadingImage;
        var timestamp = (new Date()).getTime().toString() + Math.random() * (100 - 1) + 1;
        setTimeout(function () { imgObj.src = targetSrc + "&aa=" + timestamp; }, 500);
    }
}

//图片加载错误触发的事件
function ImageError(imgObj) {
    var $this = this;
    if (imgObj != null) $this = imgObj;
    var divID = $this.getAttribute("divID");
    var divObj = document.getElementById(divID);
    $this.parentNode.style.display = "none";
    var hrefID = $this.parentNode.getAttribute("id");
    var errorTextObj = document.getElementById(divID + "_" + hrefID);
    if (errorTextObj != null) {
        errorTextObj.style.display = "";
    }
    else {
        divObj.innerHTML += errorText.replace(/\{divID\}/g, divID).replace(/\{hrefID\}/g, hrefID).replace(/\{imgID\}/g, $this.getAttribute("id"));
    }
}

//加载真正的图片
function DillWithAllImages() {
    var elements = document.getElementsByTagName("img");
    for (var i = 0; i < elements.length; i++) {
        ///加载失败时的默认图
        //        var errorImgSrc = elements[i].getAttribute("errorImgSrc");
        //        if (typeof (errorImgSrc) != "undefined")
        //            elements[i].onerror = function () {
        //                this.src = errorImgSrc;
        //                this.style.width = "154px";
        //                this.style.height = "51px";
        //                this.onerror = null;
        //            }
        //改成加载图片失败时，显示文字，用于可以手动重新刷新
        var divID = elements[i].getAttribute("divID");
        if (typeof (divID) != "undefined")
            elements[i].onerror = function () { ImageError(this); };
        ///延迟加载
        var isImageNeedLoad = elements[i].getAttribute("IsNeedLoad");
        if (typeof (isImageNeedLoad) != "undefined" &&
                    (isImageNeedLoad == "true" || isImageNeedLoad == true)) {
            elements[i].src = elements[i].getAttribute("targetSrc");
            elements[i].IsNeedLoad = false;
        }
    }
}
